<template>
  <div class="login">
    <van-nav-bar
      title="极客园-登录"
      left-text="返回"
      left-arrow
      @click-left="$router.go(-1)"
    />
    <van-form @submit="onSubmit">
      <van-field
        v-model="user.mobile"
        name="mobile"
        label="手机号"
        placeholder="请输入11位手机号"
        required
        :rules="[
          {
            required: true,
            message: '请正确输入手机号',
            pattern: /^1[3-9]\d{9}$/,
          },
        ]"
      />
      <van-field
        v-model="user.code"
        type="password"
        name="code"
        label="密码"
        placeholder="请输入6位密码"
        required
        :rules="[
          { required: true, message: '请正确输入密码', pattern: /^\d{6}$/ },
        ]"
      />
      <div style="margin: 16px">
        <van-button
          round
          block
          type="info"
          native-type="submit"
          color="#c00000"
          :disabled="isLoading"
          :loading="isLoading"
          loading-text="登录中..."
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>

<script>
import { getAuthorizations } from "@/api";
import { setToken } from "@/utils/token";
export default {
  name: "GoLogin",
  components: {},
  data() {
    return {
      user: {
        mobile: "18676225176",
        code: "246810",
      },
      // 加载提示
      isLoading: false,
    };
  },
  methods: {
    // 点击登录事件
    async onSubmit(value) {
      this.isLoading = true;
      try {
        const res = await getAuthorizations({
          mobile: value.mobile,
          code: value.code,
        });
        setToken(res.data.token);
        // console.log(res)
        this.$toast.success("登陆成功！");

        this.$router.replace(this.$route.query.path || '/home');
      } catch (error) {
        this.$toast.fail("账号或密码错误！");
      }
      this.isLoading = false;
    },
  },
};
</script>

<style lang="less" scoped>
.login {
  // padding: 10px;
  .van-form {
    padding: 10px;
  }
}
</style>
